package graphic.dialogs;

import graphic.FilesTabbedPanel;
import graphic.MainFrame;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import utility.GraphicUtilsFactory;
import core.Function;

public class DeleteFileDialog extends AbstractDialog
{
	JTextField fileName;
	JTextField programName;
	
	public DeleteFileDialog()
	{	
		super("Delete a File", "Delete File", 450, 200);
	}
	
	public void createPanel()
	{
		super.setPanel(new JPanel(new GridBagLayout()));
		
		JPanel panel = super.getPanel(); 
		
		this.programName =  new JTextField(FilesTabbedPanel.getInstance().getSelectedTab());
		this.programName.setEditable(false);

		if(this.programName.equals(FilesTabbedPanel.WELCOMETAB))
		{
			JOptionPane.showMessageDialog(this, "You can't delete a File if selected tab is Welcome!", "Error creating a file!", JOptionPane.ERROR_MESSAGE);
			super.dispose();
			
			return;
		}
		
		if(this.programName.getText().equals(FilesTabbedPanel.WELCOMETAB))
		{
			JOptionPane.showMessageDialog(this, "You can't delete a File if selected tab is Welcome!", "Error creating a file!", JOptionPane.ERROR_MESSAGE);
			super.haveToBeClosedCauseError = true;
			
			return;
		}

		this.fileName = new JTextField(FilesTabbedPanel.getInstance().getTabs().get(this.programName.getText()).getSelectedFileName());
		this.fileName.setEditable(false);

		if(this.fileName.getText().isEmpty())
		{
			JOptionPane.showMessageDialog(this, "No File has been selected!", "Error creating a file!", JOptionPane.ERROR_MESSAGE);
			super.haveToBeClosedCauseError = true;
			
			return;
		}
				
		GraphicUtilsFactory.setTitleAndBorderToComponent(this.programName, "Program Name");		
		GraphicUtilsFactory.setTitleAndBorderToComponent(this.fileName, "File Name");		
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.CENTER;
		c.insets = new Insets(5, 10, 5, 10);
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		panel.add(new JLabel("Are you sure to delete this file?"), c);
		
		c.gridx = 0;
		c.gridy = 1;
		panel.add(this.programName, c);		

		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		panel.add(this.fileName, c);
		
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 1;
		panel.add(super.getOkButton(), c);

		c.gridx = 1;
		c.gridy = 3;
		panel.add(super.getExitButton(), c);
		
	}

	@Override
	public boolean checkOkFunction() {
		if(this.fileName.getText().trim().equals(""))
		{
			JOptionPane.showMessageDialog(this, "Please select a file to delete!", "Error deleting a file!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		return true;
	}

	@Override
	public void okFunction()
	{
		if(Function.removeFile(this.fileName.getText(), this.programName.getText()))
		{
			super.dispose();
			MainFrame.getInstance().showMessage("File Deleted!", JOptionPane.INFORMATION_MESSAGE);
			FilesTabbedPanel.getInstance().getTabs().get(FilesTabbedPanel.getInstance().getSelectedTab()).createTable();
		} else {
			super.dispose();
			MainFrame.getInstance().showMessage("File NOT Deleted!", JOptionPane.ERROR_MESSAGE);
		}		
	}
}